package com.thysdy.flowable.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.thysdy.flowable.enums.CodeEnum;
import com.thysdy.flowable.exception.MyException;
import lombok.Data;

@Data
public class ReturnVo<T> extends BaseReturnVo<T> {
    @JsonIgnore
    private CodeEnum codeEnum;

    public ReturnVo() {
        super();
    }

    public ReturnVo(CodeEnum codeEnum) {
        super(codeEnum.getCode(), codeEnum.getMessage());
        this.codeEnum = codeEnum;
    }

    public ReturnVo(CodeEnum codeEnum, T data) {
        super(codeEnum.getCode(), codeEnum.getMessage(), data);
        this.codeEnum = codeEnum;
    }

    public ReturnVo(Integer code, String message) {
        super(code, message);
    }

    public ReturnVo(Integer code, String message, T data) {
        super(code, message, data);
    }

    public static <T>ReturnVo<T> success() {
        return new ReturnVo<T>(CodeEnum.success);
    }

    public static <T> ReturnVo<T> success(T data) {
        return new ReturnVo<>(CodeEnum.success, data);
    }

    public static <T> ReturnVo<T> exception(MyException e) {
        if (null != e.getCodeEnum()) {
            return new ReturnVo<>(e.getCodeEnum());
        } else {
            return new ReturnVo<>(e.getCode(), e.getMessage());
        }
    }

    public static <T> ReturnVo<T> error(Integer code){
        return new ReturnVo<T>(code,"error");
    }
    public static <T> ReturnVo<T> error(CodeEnum codeEnum){
        return new ReturnVo<T>(codeEnum.getCode(),codeEnum.getMessage());
    }

    public static <T> ReturnVo<T> error(Integer code,T data){
        return new ReturnVo<T>(code, null, data);
    }

    public static <T> ReturnVo<T> exception(Integer code, String message) {
        return new ReturnVo<>(code, message);
    }

}
